package com.ymatou.iapi.liveseller.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.AddProductsToLiveBean;
import com.ymatou.iapi.liveseller.service.AddProductsToLiveCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;

/**
 * 添加商品到直播 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AddProductsToLive {
	private static AddProductsToLiveBean addproductstoliveBean;
	private static AddProductsToLiveCall addproductstoliveCall;
	private static LiveRoomWapper liveRoomWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加商品到直播");
	}

	@Before
	public void caseUp() {
		addproductstoliveBean = new AddProductsToLiveBean();
		addproductstoliveCall = new AddProductsToLiveCall();
		liveRoomWapper = new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addproductstoliveCall.getOpurl());
		liveRoomWapper=null;
	}

	@After
	public void caseDown() {
		
		Logger.end();
	}

	private void verifyDB(Integer liveid, String pid, Integer uid,Integer PutawayCount,Integer LiveProudctNum) throws Exception {
		List<Map> listm = liveRoomWapper.selectLiveproductByIdAndPid(liveid, pid);
		Logger.verifyEquals(1, listm.size(), "验证商品记录加入到数据库");
		Map pinfo = listm.get(0);
		Logger.verifyEquals(uid, pinfo.get("UserId"), "验证用户id");
		// 加入默认可售
		Logger.verifyEquals(1, pinfo.get("Status"), "验证Status");
		// 加入默认非置顶
		Logger.verifyEquals(false, pinfo.get("IsTop"), "验证IsTop");
		LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(liveid);
		Logger.verifyEquals(PutawayCount, liveroom.getPutawayCount(), "验证PutawayCount");
		Logger.verifyEquals(LiveProudctNum, liveroom.getLiveProductNum(), "验证LiveProductNum");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddProductsToLive_001() {
		Logger.start(true, "添加商品到直播成功");
		try {

			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));

			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add(pid);
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("200", addproductstoliveCall.getString("Code"), "验证Code");
			verifyDB(createlive.getId(), pid, uinfo.getUserId(),1,1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_AddProductsToLive_002() {
		Logger.start(true, "不输入liveid失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));

			// addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add(pid);
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("400", addproductstoliveCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("直播Id参数错误", addproductstoliveCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_AddProductsToLive_003() {
		Logger.start(true, "不输入Userid失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));

			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add(pid);
			addproductstoliveBean.setProductIdList(ProductIdList);
			// addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("400", addproductstoliveCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("买手Id参数错误", addproductstoliveCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AddProductsToLive_004() {
		Logger.start(true, "liveid和userid不匹配失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));

			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add(pid);
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId() + 1);
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("201", addproductstoliveCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("无权", addproductstoliveCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_AddProductsToLive_005() {
		Logger.start(true, "不输入ProductId失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));

			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			// ProductIdList.add(pid);
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("400", addproductstoliveCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("必须选择至少一个商品", addproductstoliveCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AddProductsToLive_006() {
		Logger.start(true, "添加非本人商品到直播后查询直播列表不会显示");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 2) + 1);
			Userinfo uinfo2 = list.get(0);
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));

			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add(pid);
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId() + 1);
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("201", addproductstoliveCall.getString("Code"), "验证Code");
			// TODO 这个商品不是这个用户的 添加查询查不出校验

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AddProductsToLive_007() {
		Logger.start(true, "添加不存在商品到直播后查询直播列表不会显示");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add("123");
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(createlive.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("200", addproductstoliveCall.getString("Code"), "验证Code");
			// TODO 这个商品不是这个用户的 添加查询查不出校验
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AddProductsToLive_009() {
		Logger.start(true, "status9直播推流结束添加商品失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 9);
			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add(pid);
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("201", addproductstoliveCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播已关闭或已结束", addproductstoliveCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AddProductsToLive_010() {
		Logger.start(true, "status7系统关闭添加商品失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 7);
			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add(pid);
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("201", addproductstoliveCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播已关闭或已结束", addproductstoliveCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AddProductsToLive_011() {
		Logger.start(true, "status8未推流关闭直播添加商品失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 8);
			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add(pid);
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("201", addproductstoliveCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播已关闭或已结束", addproductstoliveCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AddProductsToLive_012() {
		Logger.start(true, "status10运营关闭直播添加商品失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 10);
			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add(pid);
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("201", addproductstoliveCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播已关闭或已结束", addproductstoliveCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddProductsToLive_013() {
		Logger.start(true, "添加多个商品到直播成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			String pid2 =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid2);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));

			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add(pid);
			ProductIdList.add(pid2);
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("200", addproductstoliveCall.getString("Code"), "验证Code");
			for (String pids : ProductIdList) {
				verifyDB(createlive.getId(), pids, uinfo.getUserId(),ProductIdList.size(),ProductIdList.size());
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddProductsToLive_014() {
		Logger.start(true, "添加商品超限失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			int count = 51;
			for (int i = 0; i < count; i++) {
				String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			addproductstoliveBean.setLiveId(createlive.getId());
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("201", addproductstoliveCall.getString("Code"), "验证Code");
			Logger.verifyEquals("待加入商品已超出直播商品上限", addproductstoliveCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddProductsToLive_015() {
		Logger.start(true, "多次添加商品超限失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			List<String> ProductIdList2 = new ArrayList<>();
			int count = 26;
			for (int i = 0; i < count; i++) {
				String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			
			for (int i = 0; i < count; i++) {
				String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				ProductIdList2.add(pid);
			}
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			addproductstoliveBean.setLiveId(createlive.getId());
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			Logger.verifyEquals("200", addproductstoliveCall.getString("Code"), "验证Code");
			addproductstoliveBean.setProductIdList(ProductIdList2);
			addproductstoliveCall.callService();
			Logger.verifyEquals("201", addproductstoliveCall.getString("Code"), "验证Code");
			Logger.verifyEquals("待加入商品已超出直播商品上限", addproductstoliveCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddProductsToLive_016() {
		Logger.start(true, "重复添加商品到直播之会有1条记录");
		try {

			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));

			addproductstoliveBean.setLiveId(createlive.getId());
			List<String> ProductIdList = new ArrayList<>();
			ProductIdList.add(pid);
			addproductstoliveBean.setProductIdList(ProductIdList);
			addproductstoliveBean.setUserId(uinfo.getUserId());
			addproductstoliveCall.setData(addproductstoliveBean);
			addproductstoliveCall.callService();
			addproductstoliveCall.callService();
			Logger.verifyEquals("200", addproductstoliveCall.getString("Code"), "验证Code");
			verifyDB(createlive.getId(), pid, uinfo.getUserId(),1,1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}